package com.huang.transport.netty.client;

import com.huangwei.entity.RpcResponse;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class UnprocessedRequests {
    private static ConcurrentHashMap<String, CompletableFuture<RpcResponse>> unprocessedResponseFutures = new ConcurrentHashMap<>();

    public void put(String resquestId,CompletableFuture<RpcResponse> future){
        unprocessedResponseFutures.put(resquestId,future);
    }

    public void remove(String requestId){
        unprocessedResponseFutures.remove(requestId);
    }
    public void complete(RpcResponse rpcResponse){
        CompletableFuture<RpcResponse> future = unprocessedResponseFutures.remove(rpcResponse.getRequestId());

        if (null != future) {
            future.complete(rpcResponse);
        } else {
            throw new IllegalStateException();
        }
    }
}
